/*
 * @Author: 恒恒 1067531094@qq.com
 * @Date: 2022-10-12 17:09:52
 * @LastEditors: 恒恒 1067531094@qq.com
 * @LastEditTime: 2022-10-13 21:03:34
 * @FilePath: \作业\router\loginRouter.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
//登录页面路由
const express = require('express')
const svgcaptcha = require('svg-captcha')
const fs = require('fs')

const router = express.Router()

let persons = fs.readFileSync(__dirname + "/persons.json", "utf8");
persons = JSON.parse(persons);

router.get('/login', (req, res) => {//渲染login页面
    res.render('login.html')
})

router.get('/index', (req, res) => {//渲染首页
    if (req.cookies.name) {
        res.render("index.html");
    } else {
        res.redirect('/login')
    }
})

router.get('/code', (req, res) => {//自动生成验证码功能
    let captcha = svgcaptcha.create({
        color: 'blue',
        noise: '3'
    })
    res.type('svg')
    res.send(captcha.data)
})

router.post("/login", (req, res) => {
    let { tel } = req.body;
    if (tel == "xiaoming01") {
        res.cookie("name", tel)
        res.send("登录成功")
    }
})

module.exports = router